/*
 * @Author: your name
 * @Date: 2020-04-24 11:46:38
 * @LastEditTime: 2020-04-26 15:40:55
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \odoc\stores\odocWorkflowDialog\odocTypeChangeStore.js
 */
import { observable, action } from 'mobx';
import { Form, ListViewPage, RouteLayout } from 'weaver-mobile-page';
import { Tools, Toast, Bubble } from 'weaver-mobile';
const getLabel = Tools.getLabel;

export default class OdocTypeChangeStore {
  @observable changeForm = new Form.Store({ route: '/req/odocComs' });
  @observable list = new ListViewPage.Store({ route: '/req/odocComs' });
  @observable formLayout = new RouteLayout.Store({ route: '/req/odocComs/odocTypeChangeCom' });
  @observable revisionForm = new Form.Store({ route: '/req/odocComs/odocTypeChangeCom' });
  @observable workflowId = "";
  @observable requestId = "";
  @observable f_weaver_belongto_userid = "";
  @observable f_weaver_belongto_usertype = "";
  @observable tableTitle = '';
  @observable loading = false;
  @observable clickTable = false;
  @observable messageModal = false;
  @observable msgContent = '';

  @action
  setProperty = (map) => {
    for (let i in map) {
      this[i] = map[i];
    }
  }

  @action
  getContent = () => {
    const params = {
      workflowId: this.workflowId,
      requestId: this.requestId,
    }
    Tools.callApi({ url: '/api/odoc/docTypeChange/getDocTypeChangeRevisionInfo', method: 'POST', params }).then(data => {
      const condition = [{
        title: data.changeInfoItem.title,
        items: data.changeInfoItem.items,
        defaultshow: true,
      }]
      this.changeForm.initForm(condition, '/req/odocComs');
      const conditionT = [{
        title: data.docTypeChangDataItem.title1,
        items: [],
        defaultshow: true,
      }]
      this.tableTitle = data.docTypeChangDataItem.title1;
      if (data.sessionkey) {
        this.list.getData({ dataKey: data.sessionkey });
      }
    })
  }
  @action
  getFormContent = (id, history) => {
    const params = {
      revisionId: id,
    }
    Tools.callApi({ url: '/api/odoc/docTypeChange/getIsMobileRevisionInfo', method: 'POST', params }).then(data => {
      this.clickTable = true;
      if (data.api_status) {
        this.clickTable = false;
        const condition = [{
          title: '',
          items: data.items,
          defaultshow: true,
        }]
        this.revisionForm.initForm(condition, '/req/odocComs/odocTypeChangeCom');
        history.push(this.formLayout.path);
      }
    })
  }

  @action
  handleSubmit = (onCancel) => {
    const { pass } = this.changeForm.doValidate();
    if (pass) {
      const params = {
        workflowId: this.workflowId,
        requestId: this.requestId,
        f_weaver_belongto_userid: this.f_weaver_belongto_userid,
        f_weaver_belongto_usertype: this.f_weaver_belongto_usertype,
        ...this.changeForm.formParams
      }
      this.loading = true;
      Tools.callApi({ url: '/api/odoc/docTypeChange/submitDocTypeChange', method: 'POST', params }).then(res => {
        if (res.api_status) {
          this.loading = false;
          Bubble.success(res.msg);
          onCancel();//返回流程表单页面
        } else {
          this.loading = false;
          this.msgContent = res.msg;
          this.messageModal = true;
        }
      })
    }
  }

  @action
  closeModal = () => {
    this.messageModal = false;
  }
  @action
  emptyComStatus = () => {
    this.changeForm = new Form.Store({ route: '/req/odocComs' });
    this.list = new ListViewPage.Store({ route: '/req/odocComs' });
    this.formLayout = new RouteLayout.Store({ route: '/req/odocComs/odocTypeChangeCom' });
    this.revisionForm = new Form.Store({ route: '/req/odocComs/odocTypeChangeCom' });
  }

}